import asyncio
import playwright.async_api
from playwright.async_api import async_playwright
from item.video_info import VideoInfo
import pipeline as pl
import os
import sys
from settings import ORIGIN_UP_HOME_PAGE_LINK, ORIGIN_PERSONAL_VIDEO_LINK
from settings import ORIGIN_DATA_PATH, OUTPUT_DATA_PATH
from settings import OUTPUT_UP_HOMEPAGE_DETAIL, OUTPUT_VIDEO_DETAIL
import openpyxl
import re
import time
from openpyxl import Workbook
from datetime import datetime

print(os.getcwd())

def prepare():
    wb = Workbook()
    ws = wb.active
    print("-------prepare-------")
    ws['A1'] = 'UID'
    ws['B1'] = 'VideoID'
    ws['C1'] = 'Title'
    ws['D1'] = 'Played'
    ws['E1'] = 'Liked'
    ws['F1'] = 'Coins'
    ws['G1'] = 'Share'
    ws['H1'] = 'VideoTime'
    ws['I1'] = 'Tag'
    ws['J1'] = 'PublishTime'
    ws['K1'] = 'ScrapeTime'

    try:
        wb.save(OUTPUT_DATA_PATH + OUTPUT_VIDEO_DETAIL)
    except Exception as e:
        print(e)
        wb.close()
    else:
        wb.close()

def load_file():
        wb = openpyxl.load_workbook(ORIGIN_DATA_PATH + ORIGIN_PERSONAL_VIDEO_LINK)
        ws = wb.active
        items = []
        for line in ws:
            link = str(line[0].value)
            uid = str(line[1].value)
            video_time = str(line[2].value)
            item = {}
            item['link'] = link
            item['uid'] = uid
            item['video_time'] = video_time
            items.append(item)
        return items

async def get_played(page:playwright.async_api.Page):
    q = await page.locator('.view').all_text_contents()
    if len(q) == 0:
        return 0
    ret = re.findall('\d+', q[0])
    if len(ret) > 0:
        return ret[0]
    return 0

async def get_liked(page:playwright.async_api.Page):
    q = await page.locator('.video-like-info').all_text_contents()
    if len(q) == 0:
        return 0
    ret = re.findall('\d+', q[0])
    if len(ret) > 0:
        return ret[0]
    return 0


async def get_coins(page:playwright.async_api.Page):
    q = await page.locator('.video-coin-info').all_text_contents()
    if len(q) == 0:
        return 0
    ret = re.findall('\d+', q[0])
    if len(ret) > 0:
        return ret[0]
    return 0

async def get_follow(page:playwright.async_api.Page):
    q = await page.locator('.video-fav-info').all_text_contents()
    if len(q) == 0:
        return 0
    ret = re.findall('\d+', q[0])
    if len(ret) > 0:
        return ret[0]
    return 0

async def get_share(page:playwright.async_api.Page):
    q = await page.locator('.video-share-info-text').all_text_contents()
    if len(q) == 0:
        return 0
    ret = re.findall('\d+', q[0])
    if len(ret) > 0:
        return ret[0]
    return 0

async def get_danmaku(page:playwright.async_api.Page):
    q = await page.locator('.video-info-detail-list .dm').all_text_contents()
    if len(q) == 0:
        return 0
    ret = re.findall('\d+', q[0])
    if len(ret) > 0:
        return ret[0]
    return 0

async def get_publish_time(page:playwright.async_api.Page):
    q = await page.locator('.pubdate-text').all_text_contents()
    if len(q) == 0:
        return 0
    return q[0]

async def extract_video_detail(context: playwright.async_api.BrowserContext, link, uid, video_time):
    if not link:
        return
    page = await context.new_page()
    await page.route("**/*", lambda route: route.abort()
    if route.request.resource_type == "image"
    else route.continue_())
    await page.goto(link, wait_until="networkidle")
    print(link)

    VIDEO_ID_POS = 4
    title = await page.locator('.video-title').text_content()
    vid = str(link).split('/')[VIDEO_ID_POS]
    played = await get_played(page)
    danmaku = await get_danmaku(page)
    liked = await get_liked(page)
    coins = await get_coins(page)
    follow = await get_follow(page)
    share = await get_share(page)
    tag = await page.locator('.tag').all_text_contents()
    publish_time = await get_publish_time(page)
    scrape_time = datetime.now().strftime("%Y-%m-%d, %H:%M:%S")

    item = VideoInfo(uid, vid, title, played, video_time, danmaku, liked, coins, follow, share, tag, publish_time, scrape_time)
    await page.close()
    return item

async def fetch(context):
    items = load_file()
    page = await context.new_page()
    prepare()
    try:
        for i in range(len(items)):
            url = items[i]['link']
            uid = items[i]['uid']
            video_time = items[i]['video_time']
            item = await extract_video_detail(context, url, uid, video_time=video_time)
            pl.add_video_detail_item(OUTPUT_VIDEO_DETAIL, item)

    except playwright.async_api.TimeoutError as e:
        print(e)
    else:
        print('over')
        await page.close()

async def bilibili() -> None:
    async with async_playwright() as p:
        browser = await p.chromium.launch(headless=False)
        context = await browser.new_context()
        await fetch(context=context)
        await context.close()
        await browser.close()

def main(argv):
    loop = asyncio.get_event_loop()
    loop.run_until_complete(bilibili())


if __name__ == "__main__":
    main(sys.argv)

